const { SuccessSend } = require("../utils/response");
const BannerModel = require("../models/banner");

class BannerController {
    static async create(req, res, next) {
        try {
            const banner = await BannerModel.create({
                title: req.body.title,
                img: req.body.img,
                url: req.body.url,
            });

            return SuccessSend(res, {
                success: true,
                message: "创建成功",
                data: banner,
            });
        } catch (error) {
            next(error);
        }
    }

    static async list(req, res, next) {
        try {
            const banners = await BannerModel.find({ status: 0 });
            return SuccessSend(res, {
                success: true,
                message: "获取成功",
                data: banners,
            });
        } catch (error) {
            next(error);
        }
    }

    static async getAll(req, res, next) {
        try {
            const banners = await BannerModel.find({}).select(
                "+title +sort +status +createAt"
            );
            return SuccessSend(res, {
                success: true,
                message: "获取成功",
                data: banners,
            });
        } catch (error) {
            next(error);
        }
    }
}

module.exports = BannerController;
